const jwt = require('jsonwebtoken');
module.exports = () => {
    return async (ctx,next) => {
        let verifyArr = ['/api/userlist','/api/userinfo'];

        if(verifyArr.includes(ctx.request.url)){
            //校验
            let token = ctx.get('authority');
            try{
                jwt.verify(token,'lixd');
                await next();
            }catch(e){
                if(e.name === 'TokenExpiredError' || e.name === 'JsonWebTokenError'){
                    ctx.status = 401;
                    ctx.body  ={
                        code:7,
                        msg:'鉴权失败'
                    }
                }else{
                    ctx.body = {
                        code:5,
                        msg:e
                    }
                }
            }
        }else{
            //不校验
            await next();
        }
    }
}